import java.util.HashMap;

class CopyRandomList {
    static class Node {
        public int val;
        public Node next;
        public Node random;

        public Node() {

        }

        public Node(int val) {
            this.val = val;
        }
    }

    public Node copyRandomList(Node head) {
        HashMap<Node,Node> hashMap = new HashMap<>();
        Node cur = head;
        while(cur != null) {
            Node newnode = new Node(cur.val);
            hashMap.put(cur,newnode);
            cur = cur.next;
        }
        cur = head;
        while(cur != null) {
            hashMap.get(cur).next = hashMap.get(cur.next);
            hashMap.get(cur).random = hashMap.get(cur.random);
            cur = cur.next;
        }
        return hashMap.get(head);

    }
}